--- title: Retrieval keywords: fastai sidebar: home_sidebar summary: "Module, containing the image retrieval by the global GeM descriptor similarity. Contains the code borrowed from https://github.com/filipradenovic/cnnimageretrieval-pytorch" description: "Module, containing the image retrieval by the global GeM descriptor similarity. Contains the code borrowed from https://github.com/filipradenovic/cnnimageretrieval-pytorch" nb_path: "retrieval.ipynb" ---
from local_feature_tutorial.datasets import *
arch = 'resnet50'
gemnet = get_pretrained_retrieval_network(arch)
from local_feature_tutorial.datasets import *
oxfordimages = get_all_images_in_subdirs('data/oxford5k')
oxford_features= f'data/oxford_{arch}.pth'
IR = ImageRanker(gemnet, transforms=transforms.Compose(
[transforms.ToPILImage(),
transforms.Resize(256),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])]))
IR.process_db_images(oxfordimages, '.',
do_diffusion=False, on_gpu=True, db_save_fname=oxford_features)
Lets search for the similar images with a global descriptor
import cv2
import matplotlib.pyplot as plt
from local_feature_tutorial.visualization import *
query_fname = oxfordimages[3073]
img = cv2.cvtColor(cv2.imread(query_fname), cv2.COLOR_BGR2RGB)
top_k = IR.get_similar(img, 12)
visualize_grid([x.replace('../','') for x in top_k['paths']])
plt.show(block=True)
As we can see, results are quite good, but some of wrong images are in the front of the good correct. Let's try to fix it with the local features
from local_feature_tutorial.wbs import *
Now we will match the images in the shortlist and re-order them based on the number of inliers
wbs = TwoViewMatcher(detector = cv2.SIFT_create(4000, contrastThreshold=-10000,
edgeThreshold=-10000),
descriptor = HardNetDesc(),
matcher=SNNMMatcher(0.9),
geom_verif=degensac_Verifier())
topk_verif = IR.spatially_verify(query_fname, top_k, wbs, False)
print (topk_verif['num_inl'])
visualize_grid(topk_verif['paths'])
Now the same, but with matches visualization
topk_verif = IR.spatially_verify(query_fname, top_k, wbs, True)